//
// See the file COPYING for redistribution information.
//

#include <iostream>
#include <cstdio>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <Naipax.hpp>

using namespace std;
using namespace Naipax;

class NumstrScheduler {
    public:
    uint32_t operator()(const string &rData,
                        uint32_t PriorityLevels) {
        if (*(rData.end()-1) >= '1' && *(rData.end()-1) < '5') {
            return 0;
        }
        else {
            return PriorityLevels - 1;
        }
    }
};


int main() {
    cerr << "Test for the PpqScheduler module" << endl;

    srand((unsigned int) time(NULL));
    system("rm -rf Ppq_test_dir");
    try {
        PpqScheduler<NumstrScheduler> Scheduler("Ppq_test_dir", 2);

        const uint32_t ShiftBits = 10;
        for (uint32_t i = 0; i < (1 << ShiftBits); i++) {
            char Buf[16];
            snprintf(Buf, 16, "%u", i);
            string BufStr(Buf);
            try {
                Scheduler.Feed(BufStr);
            }
            catch (Naipax::Error &E) {
                cerr << E.What() << endl;
            }
        }

        vector<string> DataVector;
        while (1) {
            Scheduler.Queue()->Dequeue(DataVector, 100);
            if (DataVector.empty() == true) {
                break;
            }
            for (vector<string>::iterator DataVectorIter = DataVector.begin();
                 DataVectorIter != DataVector.end(); DataVectorIter++) {
                cout << *DataVectorIter << " ";
            }
        }
        cout << endl;
    }
    catch (Naipax::Error &E) {
        NAIPAX_PERROR(E.What());
        return -1;
    }

    return 0;
}
